package com.zentrale1.noodlemail.actions;

import java.io.File;

import com.zentrale1.noodlemail.Noodlemail;

/*
 * The MIT License
 * 
 * Copyright (c) 2010 Andreas Fleig
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *
 */


/**
 * Moves messages to the best matching folder as determined by the classifier
 * @author afleig
 *
 */
public class MoveAction extends Action
{
	String path;

	public MoveAction(Noodlemail noodle) 
	{
		super(noodle);
		path = noodle.getMaildir().getPath();
	}

	
	@Override
	public String getName() 
	{
		return "move";
	}

	
	@Override
	public void perform(String subPath, String mailClass) 
	{
		// TODO check if message already is in correct directory
		File mailFile = new File(path + "/" + subPath);
		File targetFile = getTargetFile(mailFile, subPath, mailClass);
		mailFile.renameTo(targetFile);
		Noodlemail.log("  Message moved to "+targetFile.getAbsolutePath());
	}
	
	
	/**
	 * Returns the File to move to 
	 * @param mailFile File of the message to move
	 * @param subPath sub-path of the message inside the maildir
	 * @param mailClass class (^= folder) of the message as determined by 
	 *                  classifier
	 * @return
	 */
	private File getTargetFile(File mailFile, String subPath, String mailClass)
	{
		String baseName = mailFile.getName();
		String courierFolder = mailFile.getParentFile().getName();
		
		// check if courier folders are used
		if (courierFolder.equals("new") || courierFolder.equals("cur") ||
				courierFolder.equals("tmp")) {
			courierFolder = "/" + courierFolder;
		} else {
			courierFolder = "";
		}
		
		
		// special case for "inbox" (you don't have a subfolder called "inbox",
		// do you...?)
		if (mailClass.equals("inbox"))
			mailClass = "";
		
		// create target file name
		File targetFile = new File(path + "/" + mailClass + courierFolder 
				+ "/" + baseName);
		
		if (targetFile.exists()) {
			if (baseName.matches("[0-9]+\\.[^\\.]+\\.[^\\.]+")) {
				// if filename is in courier format, change 2nd part
				String[] nameParts = baseName.split("\\.");
				int i=0;
				while (targetFile.exists()) {
					String newName = path + "/" + mailClass + courierFolder 
						+ "/" + nameParts[0] + "." + nameParts[1] + i + "." 
						+ nameParts[2];
					i++;
					targetFile = new File(newName);
				}
			} else {
				// else, add _<number> to the name
				Noodlemail.log("  Mail filename is not in courier format");
				int i=0;
				while (targetFile.exists()) {
					String newName = path + "/" + mailClass + courierFolder 
						+ "/" + baseName + "_" + i;
					targetFile = new File(newName);
					i++;
				}
			}
		}
		return targetFile;
	}

}
